package com.damie.web.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.damie.common.dao.Site;
import com.damie.common.paramer.SiteParamer;
import com.damie.web.exception.MyException;
import com.damie.web.mapper.SiteMapper;
import com.damie.web.service.SiteService;
import com.damie.web.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.MessageConst;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ls
 * @since 2021-01-18
 */
@Service
public class SiteServiceImpl extends ServiceImpl<SiteMapper, Site> implements SiteService {
    @Autowired
    private SiteMapper siteMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 增加回收站点操作 判断站点是否存在 电话是否被注册 并序列化存入redis设置7天过期等待管理员审核
     * @param siteParamer
     * @return
     */
    public int addSite(SiteParamer siteParamer,HttpServletRequest httpServletRequest){
        Site site = new Site();
        BeanUtils.copyProperties(siteParamer,site);
        QueryWrapper<Site> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("site_name",site.getSiteName());
        List<Site> siteList = siteMapper.selectList(queryWrapper);
        if (!ObjectUtils.isEmpty(siteList)){
            throw new MyException(500,"站点已存在");
        }else {
            siteParamer.setSiteName(site.getSiteName());
        }
        QueryWrapper<Site> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("site_tel",site.getSiteTel());
        System.out.println(site.getSiteTel());
        Site site1 = siteMapper.selectOne(queryWrapper1);
        System.out.println(site1);
        if (!ObjectUtils.isEmpty(site1)) {
            throw new MyException(500, "电话号码已被注册");
        }
        siteParamer.setSiteTel(site.getSiteTel());
        Calendar.getInstance().setTimeInMillis(System.currentTimeMillis());
        siteParamer.setCreateTime(Calendar.getInstance().getTime());
        siteParamer.setSiteStatus(0);

        String token = httpServletRequest.getHeader("token");
        String userId = JwtUtil.getUserId(token);
        System.out.println("用户id:++++++++"+userId);
        siteParamer.setUserId(userId);//从网关获取token中的userId
        siteParamer.setSiteLongitude(106.531718);
        siteParamer.setSiteLatitude(29.590958);
        siteParamer.setSiteType(1);

//        siteParamer.setUserId("1351497901649453058");

        BeanUtils.copyProperties(siteParamer,site);
        System.out.println(site);
        int insert = siteMapper.insert(site);
        if (insert>0){
//            System.out.println("开始发送消息队列");
//            Message<Site> message= MessageBuilder.withPayload(site)
//                    .setHeader(MessageConst.PROPERTY_KEYS,site.getSiteId())
//                    .build();//发送消息队列
//            SendResult key = rocketMQTemplate.syncSendOrderly("ls:group1", message, "key");
//            System.out.println("发送消息:"+key);

//            JSONObject jsonObject = JSONObject.parseObject(site.toString());
//            redisTemplate.opsForList().rightPush("site",site);
//            redisTemplate.expire("site",7, TimeUnit.DAYS);
//            List list = redisTemplate.opsForList().range("site", 0, -1);
//            System.out.println("redis中取出的站点申请集合"+list);
        }
        return insert;
    }

    /**
     * 修改站点信息操作,站点状态为1为逻辑删除视为站点已经下线则不能修改
     * 如果站点同名也不能修改
     * @param siteParamer
     * @return
     */
    public int updSite(SiteParamer siteParamer){
        Site site = new Site();
        BeanUtils.copyProperties(siteParamer,site);
        QueryWrapper<Site> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("site_id",site.getSiteId());
        Site site1 = siteMapper.selectOne(queryWrapper);
        System.out.println(site1);
        if (site1.getSiteStatus()==0){
            throw new MyException(500,"站点正在申请中,无法修改");
        }
        QueryWrapper<Site> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("site_name",site.getSiteName());
        Site site2 = siteMapper.selectOne(queryWrapper1);
        if (!ObjectUtils.isEmpty(site2)){
            throw new MyException(500,"站点名字已被占用");
        }
        BeanUtils.copyProperties(siteParamer,site);
        int update = siteMapper.update(site, queryWrapper);
        return update;
    }

    /**
     * 只能查询到已上线的站点
     * @param siteParamer
     * @return
     */
    public List<Site> findAll(SiteParamer siteParamer){
        Site site = new Site();
        BeanUtils.copyProperties(siteParamer,site);
        List<Site> sites = siteMapper.selectList(null);
        System.out.println(sites);
        return sites;
    }

    /**
     * 站点下线,站点逻辑删除,数据库存在,但无法查询修改了
     * @param siteParamer
     * @return
     */
    public int delete(SiteParamer siteParamer){
        Site site = new Site();
        BeanUtils.copyProperties(siteParamer,site);
        QueryWrapper<Site> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("site_id",site.getSiteId());
        Site site1 = siteMapper.selectOne(queryWrapper);
        System.out.println(site1);
        if (site1.getSiteStatus()==0){
            throw new MyException(500,"站点已经下线无需重复删除");
        }
        siteParamer.setSiteStatus(1);
        BeanUtils.copyProperties(siteParamer,site);
        int delete = siteMapper.delete(site);
        return delete;
    }
}
